package com.wat.aop.example2;

import org.aopalliance.aop.Advice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;

import java.lang.reflect.Method;

 /**
 * advisor 则是对advice、pointcut的整合。
 */
//工人增强器
 @SuppressWarnings("serial")
public class WorkerAdvisor extends AbstractPointcutAdvisor {

	/**
	 * advice接口的实现类：描述了aop代理要做什么事；
	 */
	@Override
	public Advice getAdvice() {
		return new WorkerAdvice();
	}

	/**
	 * pointcut接口的实现类：描述了aop代理在哪里做事；
	 */
	@Override
	public Pointcut getPointcut() {
		String pattern = "com.wat.aop.example2.*";
		JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
		pointcut.setPattern(pattern);
		return pointcut;
	}

	static class WorkerAdvice implements AfterReturningAdvice, MethodBeforeAdvice {
		@Override
		public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
			System.out.println("21:00 - 滴，下班：没有困难的工作，只有勇敢的打工人！");
		}
		@Override
		public void before(Method method, Object[] args, Object target) throws Throwable {
			System.out.println("9:00 - 滴，上班：早安打工人！");
		}
	}
}


